package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;


    @RequestMapping("/getOrderSettingByMonth.do")
    public Result getOrderSettingByMonth(String date){
        try{
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    @RequestMapping("/upload.do")
    public Result upload(@RequestParam("excelFile") MultipartFile multipartFile){
        try{
            List<String[]> list = POIUtils.readExcel(multipartFile);    //把表格转换为List

            /*  常规写法  */
//            ArrayList<OrderSetting> orderSettings = new ArrayList<>();
//            for (String[] strings : list) {
//                //添加数据
//                orderSettings.add(new OrderSetting(new Date(strings[0]),Integer.valueOf(strings[1])));
//            }
            /* stream流链式编程写法 */
            List<OrderSetting> orderSettings =
                    list.stream().map((String[] str) -> new OrderSetting(new Date(str[0]), Integer.valueOf(str[1])))
                            .collect(Collectors.toList());

            //调用业务层添加数据 或者更新数据
            orderSettingService.add(orderSettings);
            return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/editNumberByDate.do")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try{
            orderSettingService.edit(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
     }

     @RequestMapping("/deleteByDate.do")
     @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Result deleteByDate(String date){
         try{
             orderSettingService.deleteByDate(date);
             return new Result(true,MessageConstant.DELETE_ORDERSETTING_SUCCESS);
         }catch (Exception e){
             e.printStackTrace();
             return new Result(false,MessageConstant.DELETE_ORDERSETTING_FAIL);
         }
     }
}
